import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.loiot.commons.utils.DateUtil;
import com.loiot.commons.utils.EncryptUtil;
import com.loiot.commons.utils.FileUtil;
import com.loiot.commons.utils.IOUtil;
import com.loiot.commons.utils.JsonUtil;
import com.loiot.commons.utils.MathUtil;
import com.loiot.commons.utils.MobileUtil;
import com.loiot.commons.utils.RandomUtil;
import com.loiot.commons.utils.ReflectUtil;
import com.loiot.commons.utils.StringUtil;
import com.loiot.commons.utils.User;
import com.loiot.commons.utils.UserInfo;

/**
 * 演示测试
 * 
 * @author zhengrunjin
 * 
 */
public class Demo {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		// DateUtil
		// DateUtilDemo();

		// StringUtil
		// StringUtilDemo();

		// MathUtil
		// MathUtilDemo();

		// RandomUtil
		// RandomUtilDemo();

		// EncryptUtil
		// EncryptUtilDemo();

		// JsonUtil
		// JsonUtilDemo();

		// ReflectUtil
		// ReflectUtilDemo();

		// FileUtil
		// FileUtilDemo();

		// IOUtil
		// IOUtilDemo();

		// MobileUtil
		// MobileUtilDemo();
	}

	private static void MobileUtilDemo() {
		System.out.println(MobileUtil.isMobile("18512344321"));
	}

	private static void IOUtilDemo() throws IOException, FileNotFoundException {
		// readLines
		List<String> list = IOUtil.readLines(FileUtil.readFileToInputStream("/Users/zhengrunjin/workspace/test/test.txt"), "UTF-8");
		for (String str : list) {
			System.out.println(str);
		}
		// write
		FileUtil.copyFile("/Users/zhengrunjin/workspace/test/test.txt", "/Users/zhengrunjin/workspace/test/test1.txt");
		IOUtil.write("改成一句话。", new FileOutputStream(new File("/Users/zhengrunjin/workspace/test/test1.txt")), "UTF-8");
		// copy
		IOUtil.copy(FileUtil.readFileToInputStream("/Users/zhengrunjin/workspace/test/test1.txt"), System.out);
	}

	private static void FileUtilDemo() throws IOException {
		// createFile
		File file = FileUtil.createFile("/Users/zhengrunjin/workspace/test/1.txt");
		// copyFile
		FileUtil.copyFile(file, new File("/Users/zhengrunjin/workspace/test/2.txt"));
		// deleteFile
		FileUtil.deleteFile(file);
		FileUtil.deleteFile("/Users/zhengrunjin/workspace/test/2.txt");
		// createDirectory
		FileUtil.createDirectory("/Users/zhengrunjin/workspace/test/1/2/3");
		// moveDirectory
		FileUtil.moveDirectory("/Users/zhengrunjin/workspace/test/1/2/3", "/Users/zhengrunjin/workspace/test/1/3");
		// copyDirectory
		FileUtil.copyDirectory("/Users/zhengrunjin/workspace/test/1", "/Users/zhengrunjin/workspace/test/");
		// deleteDirectory
		FileUtil.deleteDirectory("/Users/zhengrunjin/workspace/test/1");
		FileUtil.deleteDirectory("/Users/zhengrunjin/workspace/test/2");
		FileUtil.deleteDirectory("/Users/zhengrunjin/workspace/test/3");
	}

	private static void ReflectUtilDemo() throws IllegalAccessException, InvocationTargetException {
		User user = new User();
		user.setAge(20);
		user.setName("Rankin");
		user.setBirthDay(DateUtil.toDate("1987-06-29"));
		UserInfo userInfo = new UserInfo();
		userInfo.setEmail("rankin@qq.com");
		user.setUserInfo(userInfo);
		// getFieldValue
		System.out.println(ReflectUtil.getFieldValue(user, "name"));
		// invokeMethod
		System.out.println(ReflectUtil.invokeMethod(user, "myName", null, null));
		// setFieldValue
		ReflectUtil.setFieldValue(user, "name", "Jack");
		System.out.println(user.getName());
		// getField
		Field name = ReflectUtil.getField(user, "age");
		System.out.println(name.getType());
		// get Method
		Method myName = ReflectUtil.getMethod(user, "myName", null);
		System.out.println(myName.getReturnType());
	}

	private static void DateUtilDemo() {
		// toDate
		Date date = DateUtil.toDate("2013-01-07");
		System.out.println(date);
		// toString
		System.out.println(DateUtil.toString(date, "yyyy/MM/dd"));
		// getDay
		System.out.println(DateUtil.getDay(date));
		System.out.println(DateUtil.getDayOfMonth(date));
		System.out.println(DateUtil.getDayOfWeek(date));
		System.out.println(DateUtil.getDayOfYear(date));
		// addDays
		Date date1 = DateUtil.addDays(date, 31);
		System.out.println(DateUtil.toString(date1, "yyyy/MM/dd"));
	}

	private static void StringUtilDemo() {
		// isEmpty
		String str1 = "";
		System.out.println(StringUtil.isBlank(str1));
		System.out.println(StringUtil.isEmpty(str1));
		// isBlank
		String str2 = " ";
		System.out.println(StringUtil.isBlank(str2));
		System.out.println(StringUtil.isEmpty(str2));
		// isNumeric
		System.out.println(StringUtil.isNumeric("12.3"));
		System.out.println(StringUtil.isNumeric("abc"));
		// join
		List<String> name = new ArrayList<String>();
		name.add("Bill Gates");
		name.add("Jack Welch");
		name.add("Jason Bourne");
		System.out.println(StringUtil.join(name));
		System.out.println(StringUtil.join(name, "\r\n"));
		// split
		String numberStr = "1,2,3,4,,,5,6，，，";
		List<Integer> numbers = StringUtil.splitToInteger(numberStr, ",");
		System.out.println(StringUtil.join(numbers, "/"));
		List<String> strs = StringUtil.splitToString(numberStr, ",");
		System.out.println(StringUtil.join(strs, "-"));
		// truncate
		String str3 = "我说的这句话太长了。";
		System.out.println(StringUtil.truncate(str3, 3));
		System.out.println(StringUtil.truncate(str3, 3, "..."));
	}

	private static void MathUtilDemo() {
		// intToHex
		System.out.println(MathUtil.intToHex(15));
		// hexToInt
		System.out.println(MathUtil.hexToInt("0000000F"));
		// bytesToHex
		System.out.println(MathUtil.bytesToHex(new byte[] { 127, 0, 127 }));
		// hexToBytes
		System.out.println(MathUtil.hexToBytes("7F007F")[0] + "," + MathUtil.hexToBytes("7F007F")[1] + "," + MathUtil.hexToBytes("7F007F")[2]);
		// byteToInt
		System.out.println(MathUtil.bytesToInt(new byte[] { 0, 0, 0, -1 }));
		// intToBytes
		System.out.println(MathUtil.intToBytes(255)[0] + "," + MathUtil.intToBytes(255)[1] + "," + MathUtil.intToBytes(255)[2] + "," + MathUtil.intToBytes(255)[3]);
		// sum
		System.out.println(MathUtil.sum(new int[] { 1, 2, 3, 4 }));
		System.out.println(MathUtil.sum(new double[] { 1, 2, 3, 4 }));
		// avg
		System.out.println(MathUtil.avg(new int[] { 1, 2, 3, 4 }));
		System.out.println(MathUtil.avg(new double[] { 1, 2, 3, 4 }));
	}

	private static void RandomUtilDemo() {
		// nextInt
		System.out.println(RandomUtil.nextInt());
		for (int i = 0; i < 100; i++) {
			System.out.println(RandomUtil.nextInt(10));
		}
		System.out.println("================\r\n================\r\n================\r\n================\r\n================\r\n");
		for (int i = 0; i < 100; i++) {
			System.out.println(RandomUtil.nextInt(5, 10));
		}
		// nextString
		System.out.println("================\r\n================\r\n================\r\n================\r\n================\r\n");
		for (int i = 0; i < 100; i++) {
			System.out.println(RandomUtil.nextString(10));
		}
		System.out.println("================\r\n================\r\n================\r\n================\r\n================\r\n");
		for (int i = 0; i < 100; i++) {
			System.out.println(RandomUtil.nextString(10, "ABCD0123456789"));
		}
	}

	private static void EncryptUtilDemo() {
		String password = "admin123是";
		// Base64
		System.out.println(EncryptUtil.encodeBASE64(password));
		System.out.println(EncryptUtil.encodeBASE64(password, "GBK"));
		System.out.println(EncryptUtil.decodeBASE64(EncryptUtil.encodeBASE64(password)));
		System.out.println(EncryptUtil.decodeBASE64(EncryptUtil.encodeBASE64(password, "GBK"), "GBK"));
		// DES
		System.out.println(EncryptUtil.encodeDES(password));
		System.out.println(EncryptUtil.encodeDES(password, "GBK"));
		System.out.println(EncryptUtil.decodeDES(EncryptUtil.encodeDES(password)));
		System.out.println(EncryptUtil.decodeDES(EncryptUtil.encodeDES(password, "GBK"), "GBK"));
		// MD5
		System.out.println(EncryptUtil.md5Hex(password));
		try {
			System.out.println(EncryptUtil.md5Hex(password, "GBK"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	private static void JsonUtilDemo() throws Exception {
		User user = new User();
		user.setAge(20);
		user.setName("Rankin");
		user.setBirthDay(DateUtil.toDate("1987-06-29"));
		UserInfo userInfo = new UserInfo();
		userInfo.setEmail("rankin@qq.com");
		user.setUserInfo(userInfo);
		// toJson
		String json_user = JsonUtil.toJson(user);
		System.out.println(json_user);
		// toString
		Date birthDay = JsonUtil.toDate(json_user, "birthDay");
		System.out.println(DateUtil.toString(birthDay, "yyyy/MM/dd"));
		// toInteger
		Integer age = JsonUtil.toInteger(json_user, "age");
		System.out.println(age);
		// getFieldString
		System.out.println(JsonUtil.getFieldString(json_user, "birthDay"));
		System.out.println(JsonUtil.getFieldString(json_user, "age"));
		// toObject
		User user1 = JsonUtil.toObject(json_user, User.class);
		System.out.println(user1.getName());
		System.out.println(user1.getUserInfo().getEmail());
		// toList
		List<User> userList = new ArrayList<User>();
		userList.add(user);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("list", userList);
		String json_data = JsonUtil.toJson(data);
		System.out.println(json_data);
		List<User> userList1 = JsonUtil.toList(json_data, "list", User.class);
		System.out.println(userList1.get(0).getName());
		System.out.println(userList1.get(0).getUserInfo().getEmail());
		// writeJson
		JsonUtil.writeJson(System.out, userList);
	}
}
